﻿using System;
using System.Drawing;
using Microsoft.Office.PowerPoint.STAMP.Core;
using Microsoft.Office.PowerPoint.STAMP.Properties;
using NUnit.Framework;

namespace Microsoft.Office.PowerPoint.STAMP.Test
{
    /// <summary>
    ///This is a test class for UtilsTest and is intended
    ///to contain all UtilsTest Unit Tests
    ///</summary>
    [TestFixture]
    public class UtilsTest
    {
        #region Font Size Rule Table
        [Test()]
        public void FontSizeRuleTableLessThanFour()
        {
            CheckRange(0.0, 3.99, Utils.Font30);
        }

        [Test()]
        public void FontSizeRuleTableFourToFive()
        {
            CheckRange(4.0, 4.99, Utils.Font12);
        }

        [Test()]
        public void FontSizeRuleTableFiveToSix()
        {
            CheckRange(5.0, 5.99, Utils.Font16);
        }

        [Test()]
        public void FontSizeRuleTableSixToSeven()
        {
            CheckRange(6.0, 6.99, Utils.Font18);
        }

        [Test()]
        public void FontSizeRuleTableSevenToEigthPointFive()
        {
            CheckRange(7.0, 8.49, Utils.Font21);
        }

        [Test()]
        public void FontSizeRuleTableEigthPointFiveToNineThreeQuarters()
        {
            CheckRange(8.5, 9.74, Utils.Font26);
        }

        [Test()]
        public void FontSizeRuleTableOverNineThreeQuarters()
        {
            CheckRange(9.75, 12, Utils.Font30);
        }

        private void CheckRange(double min, double max, int expectedSize)
        {
            CheckSingle(min, expectedSize);
            CheckSingle(min + 0.1, expectedSize);
            CheckSingle(max - 0.1, expectedSize);
            CheckSingle(max, expectedSize);
        }

        private void CheckSingle(double value, int expectedSize)
        {
            Assert.AreEqual(expectedSize, Utils.DetermineFontSize(ConvertInchToPixels(value)), string.Format("Width: {0}\"", value));
        }

        private double ConvertInchToPixels(double inches)
        {
            return (inches * 72);
        }
        #endregion


        /// <summary>
        ///A test for DetermineShapeXPosition
        ///</summary>
        [Test()]
        public void DetermineShapeXPositionTest()
        {
            CheckShapeXPosition(40, 50, 0, 10);
            CheckShapeXPosition(120, 50, 80, 10);
        }

        private void CheckShapeXPosition(float expected, float containerHeight, float top, float shapeHeight)
        {
            Assert.AreEqual(expected, Utils.DetermineShapeYPosition(containerHeight, top, shapeHeight));
        }

        [Test()]
        public void DetermineShapeWidth()
        {
            var mediaWidth = 500;
            var slideWidth = 600;

            Assert.AreEqual(mediaWidth, Utils.DetermineShapeWidth(CaptionLocation.VideoBottom, slideWidth, mediaWidth));
            Assert.AreEqual(slideWidth, Utils.DetermineShapeWidth(CaptionLocation.SlideBottom, slideWidth, mediaWidth));
        }

        /// <summary>
        ///A test for DetermineShapeXPositionByLocation
        ///</summary>
        [Test()]
        public void DetermineShapeXPositionByLocationTest()
        {
            RectangleF slideRectangle = new RectangleF(0, 0, 1000, 700);
            RectangleF mediaRectangle = new RectangleF(0, 0, 400, 300);
            float shapeHeight = 30;

            CheckDetermineShapeXPositionByLocation(270, CaptionLocation.VideoBottom, slideRectangle, mediaRectangle, shapeHeight);
            CheckDetermineShapeXPositionByLocation(670, CaptionLocation.SlideBottom, slideRectangle, mediaRectangle, shapeHeight);

            mediaRectangle.Y = 10;
            CheckDetermineShapeXPositionByLocation(280, CaptionLocation.VideoBottom, slideRectangle, mediaRectangle, shapeHeight);

        }
        private void CheckDetermineShapeXPositionByLocation(float expected, CaptionLocation capLoc, RectangleF slideRectangle, RectangleF mediaRectangle, float shapeHeight)
        {
            Assert.AreEqual(expected, Utils.DetermineShapeYPositionByLocation(capLoc, slideRectangle, mediaRectangle, shapeHeight));
        }


        [Test()]
        public void ShapeNameTest()
        {
            string mediaName = "MediaName";
            string captionText = "this is my caption text";
            int start = 100;
            int end = 300;

            string shapeName = Utils.CreateShapeName(mediaName, captionText, start, end);
            Assert.AreEqual("MediaNamethisismy00:00:00.100-00:00:00.300", shapeName);

            shapeName = Utils.CreateShapeName("MediaName", "Hello World", 5);

            Assert.AreEqual("MediaNameHelloWorld00:00:00.005", shapeName);
        }

        [Test()]
        public void ShapeNameDateTest()
        {
            string mediaName = "MediaName";
            string captionText = "this is my caption text";
            TimeSpan start = new TimeSpan(0, 0, 0, 2, 050);
            TimeSpan end = new TimeSpan(0, 0, 0, 9, 123);

            string shapeName = Utils.CreateShapeName(mediaName, captionText, Convert.ToInt32(start.TotalMilliseconds), Convert.ToInt32(end.TotalMilliseconds));

            string outputName = "MediaNamethisismy" +
                                Convert.ToInt32(start.TotalMilliseconds).ToTimeSpanString(Resources.TimeSpanStringFormat) +
                                "-" +
                                Convert.ToInt32(end.TotalMilliseconds).ToTimeSpanString(Resources.TimeSpanStringFormat);
            Console.WriteLine(outputName);

            Assert.AreEqual(outputName, shapeName);
        }

        [Test()]
        public void ShowTimes()
        {
            TimeSpan start = new TimeSpan(0, 0, 0, 2, 050);
            TimeSpan end = new TimeSpan(0, 0, 0, 9, 123);

            Console.WriteLine("start TotalMilliseconds : " + start.TotalMilliseconds);
            Console.WriteLine("end TotalMilliseconds : " + end.TotalMilliseconds);

            Console.WriteLine("start ToTimeSpanString : " + Convert.ToInt32(start.TotalMilliseconds).ToTimeSpanString(Resources.TimeSpanStringFormat));
            Console.WriteLine("end ToTimeSpanString : " + Convert.ToInt32(end.TotalMilliseconds).ToTimeSpanString(Resources.TimeSpanStringFormat));
        }
    }
}
